package com.central.apps.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.central.app.common.feign.WorkflowClient;
import com.central.app.service.HandlerService;
import com.central.apps.util.CloseFlowUtil;
import com.central.apps.util.HttpUtil;
import com.central.common.util.TokenUtil;
import com.central.common.web.Result;
import io.swagger.annotations.Api;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Api(tags = "流程关闭")
@RequestMapping("/close")
public class ClossFlowController {
    @Autowired
    private HandlerService handlerService;
    @Autowired
    private WorkflowClient workflowClient;
    @Autowired
    private CloseFlowUtil closeFlowUtil;

    @RequestMapping(value = "/flow", method = RequestMethod.POST)
    public String downloadFile(HttpServletRequest request, @RequestBody Map<String, Object> params) throws Exception {

        //1 将状态改为关闭状态
        String id = MapUtils.getString(params, "id");//request.getParameter("id");
        System.out.println("id==================" + id);
        Map<String, Object> map = new HashMap<>();
        map.put("id", id);
        map.put("status", "关闭");
        handlerService.update("gq_po", map);
        //2.将流程结束，调用结束流程的接口
        String res = closeFlowUtil.closeFlow("poApp",Integer.valueOf(id));
        JSONObject json = new JSONObject();
        json.put("msg", res);
        return json.toString();
//        String sql = "select proc_ins_id from biz_flow_app where app = 'poApp' and biz_id = '" + id + "'";
//        List<Map<String, Object>> list = handlerService.findBySql(sql);
//        if (CollectionUtils.isNotEmpty(list)) {
//
//            //String accessToken = TokenUtil.getToken();
//            String procInsId = (String) list.get(0).get("proc_ins_id");
//            workflowClient.moveToEnd(procInsId);
//
//            JSONObject json = new JSONObject();
//            json.put("msg", "流程已关闭");
//            return json.toString();
//            //return Result.succeed("流程已关闭");
//        } else {
//            JSONObject json = new JSONObject();
//            json.put("msg", "流程未启动，无需关闭");
//            return json.toString();
//            // return Result.succeed("流程未启动，无需关闭");
//        }


    }

}

